package net.homeftp.heimo.weddingwidget;

import net.homeftp.heimo.weddingwidget.perf.MyPreferenceManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class WeddingWidgetProvider extends AppWidgetProvider {

	/*
	 * onReceive()
	 * 
	 * (1)broadcast when one or more AppWidget instances have been deleted
	 * ACTION:android.appwidget.action.APPWIDGET_DELETED
	 * 
	 * (2)broadcast, which is sent when the last AppWidget instance for this
	 * provider is deleted ACTION:android.appwidget.action.APPWIDGET_DISABLED
	 * 
	 * (3)broadcast when the a AppWidget for this provider is instantiated
	 * ACTION:android.appwidget.action.APPWIDGET_ENABLED
	 * 
	 * (4)broadcast when this AppWidget provider is being asked to provide
	 * RemoteViews for a set of AppWidgets
	 * ACTION:android.appwidget.action.APPWIDGET_UPDATE
	 */

	private static final String TAG = "WeddingWidgetProvider";

	@Override
	public void onReceive(Context ctx, Intent intent) {
		Log.d(TAG, "[onReceive] action : " + intent.getAction());
		super.onReceive(ctx, intent);
		String eventName = ctx.getResources().getString(
				R.string.widget_event_name);
		// DO UPDATE
		if (eventName.equals(intent.getAction())
				|| intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
				|| intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED)) {
			AppWidgetManager appWidgetManager = AppWidgetManager
					.getInstance(ctx);
			ComponentName thisAppWidget = new ComponentName(
					ctx.getPackageName(), WeddingWidgetProvider.class.getName());
			int[] appWidgetIds = appWidgetManager
					.getAppWidgetIds(thisAppWidget);
			onUpdate(ctx, appWidgetManager, appWidgetIds);

		}
	}

	@Override
	public void onEnabled(Context ctx) {
		super.onEnabled(ctx);
		Log.d(TAG, "[onEnabled]");
		// RENEW TIMMER
		ctx.startService(getWeddingService(ctx));
	}

	@Override
	public void onDisabled(Context ctx) {
		super.onDisabled(ctx);
		Log.d(TAG, "[onDisabled]");
		ctx.stopService(getWeddingService(ctx));
	}

	@Override
	public void onDeleted(Context ctx, int[] appWidgetIds) {
		Log.d(TAG, "[onDeleted]");
		for (int appWidgetId : appWidgetIds) {
			Log.d(TAG, "[onDeleted] id : " + appWidgetId);
			MyPreferenceManager sPref = new MyPreferenceManager(ctx, appWidgetId);
			sPref.removePrefForWidget();
		}
		super.onDeleted(ctx, appWidgetIds);
	}

	@Override
	public void onUpdate(Context ctx, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.d(TAG, "[onUpdate]");
		// Get all widget id(s)
		ComponentName thisWidget = new ComponentName(ctx,
				WeddingWidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		for (int widgetId : allWidgetIds) {
			Log.i(TAG, "onWidget Update ID : "+widgetId);
			
			MyPreferenceManager sPref = new MyPreferenceManager(ctx, widgetId);
			RemoteViews rv = new RemoteViews(ctx.getPackageName(),
					R.layout.widget_layout_1);

			WeddingWidgetHelper helper = new WeddingWidgetHelper(ctx, sPref);
			rv = helper.getUpdateView(rv);

			rv.setOnClickPendingIntent(R.id.entire_widget, fnGetPendingIntent(ctx,widgetId));

			// RENEW TIMMER
			ctx.startService(getWeddingService(ctx));
			
			//appWidgetManager.updateAppWidget(widgetId, rv);
			AppWidgetManager.getInstance(ctx).updateAppWidget(widgetId, rv);
			
			
		}
	}

	private PendingIntent fnGetPendingIntent(Context ctx, int appWidgetId) {
		Intent i = new Intent(ctx, WeddingWidgetActivity.class);
		Log.v(TAG,"[fnGetPendingIntent] ID : "+ appWidgetId);
		i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		
//		PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0,
//				i, PendingIntent.FLAG_UPDATE_CURRENT);
		
		PendingIntent pendingIntent = 
			PendingIntent.getActivity(ctx, appWidgetId, i, PendingIntent.FLAG_UPDATE_CURRENT);

		/*
		 * PendingIntent.FLAG_ONE_SHOT 
		 *  - 	Make new Activity
		 * PendingIntent.FLAG_UPDATE_CURRENT 
		 *  -	Reuse the old Activity
		 * PendingIntent.FLAG_CANCEL_CURRENT 
		 * 	-	If current exists, discard and create new
		 * Intent.FLAG_ACTIVITY_NO_HISTORY 
		 * 	-	your activity will not remain on the stack after another activity covers it
		 * */
		return pendingIntent;
	}

	/*
	 * Private function
	 */
	private Intent getWeddingService(Context ctx) {
		return new Intent(ctx, WeddingWidgetService.class);
	}
}